#!/usr/bin/python
# -*- coding: utf-8 -*-

from patentwars import db


class Player(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    cash = db.Column(db.Integer)
    fame = db.Column(db.Integer)
    password = db.Column(db.String)


class Technology(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    innovation = db.Column(db.Integer)
    worth = db.Column(db.Integer)


class Similarity(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    strength = db.Column(db.Integer)
    base_technology_id = db.Column(db.Integer, db.ForeignKey('technology.id'))
    base_technology = db.relationship('Technology',
                        primaryjoin=base_technology_id==Technology.id,
                                      backref=db.backref('inspires'))
    technology_id = db.Column(db.Integer, db.ForeignKey('technology.id'))
    technology = db.relationship('Technology',
                                 primaryjoin=technology_id==Technology.id,
                                 backref=db.backref('inspired_by'))


class Patent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    strength = db.Column(db.Integer)
    owner_id = db.Column(db.Integer, db.ForeignKey('player.id'))
    owner = db.relationship('Player', backref=db.backref('patents'))
    technology_id = db.Column(db.Integer, db.ForeignKey('technology.id'))
    technology = db.relationship('Technology', backref=db.backref('patents'))


class Lawsuit(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    attacker_id = db.Column(db.Integer, db.ForeignKey('patent.id'))
    attacker = db.relationship('Patent', backref=db.backref('attacking'),
                               primaryjoin=attacker_id==Patent.id)
    defender_id = db.Column(db.Integer, db.ForeignKey('patent.id'))
    defender = db.relationship('Patent', backref=db.backref('defending'),
                               primaryjoin=defender_id==Patent.id)


class Bid(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    value = db.Column(db.Integer)
    player_id = db.Column(db.Integer, db.ForeignKey('player.id'))
    player = db.relationship('Player', backref=db.backref('bids'))
    technology_id = db.Column(db.Integer, db.ForeignKey('technology.id'))
    technology = db.relationship('Technology', backref=db.backref('bids'))


class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True)
    price = db.Column(db.Integer)
    fame = db.Column(db.Integer)


class Application(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    technology_id = db.Column(db.Integer, db.ForeignKey('technology.id'))
    technology = db.relationship('Technology',
                                 backref=db.backref('applications'))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))
    product = db.relationship('Product', backref=db.backref('applications'))

